﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Model.Calendar;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;


namespace Jzy.DAL.Calendar
{
    public class JoinerDAL:BaseDAL
    {
        private string strType;

        public JoinerType JoinerType
        {
            set
            {
                strType = value.ToString();
                if (value == Model.Calendar.JoinerType.Dept)
                {
                    _tableName = OTableName.CalDept;
                }
                else
                {
                    _tableName = OTableName.CalUser;
                }
            }            
        }

        private string _tableName;

      
        public JoinerDAL()
        {
            JoinerType = JoinerType.User;
        }

        public JoinerDAL(JoinerType type)
        {
            JoinerType = type;
        }

        public void Save(int calID, List<int> lstJoinerIDs)
        {

           
            string strSql = "delete from " + _tableName + " where CalID = @p0";
            dbOper.RunSql(strSql,calID);

            for (int i = 0; i < lstJoinerIDs.Count; i++)
            {
                strSql = "insert into " + _tableName + "(CalID," + strType + "ID) values(@p0, @p1)";
                dbOper.RunSql(strSql, calID, lstJoinerIDs[i]);
            }
            
        }

        

        public List<int> GetJoiner(object calID)
        {
            string strSql = "select " + strType + "ID from " + _tableName + " where CalID=@p0";          
            List<int> lstJoinerIDs = dbOper.Query<int>(strSql, calID);
            return lstJoinerIDs;
        }

        public List<int> GetCalIDs(object joinerID)
        {
            string strSql = "select CalID from " + _tableName + " where " + strType + "ID = @p0";           
            List<int> lstCalIDs = dbOper.Query<int>(strSql, joinerID);
            return lstCalIDs;
        }

        public List<int> GetCalIDs(List<int> lstJoinerIDs)
        {
            string strJoinerIDs = ConvertUtil.ConvertToDbStr(lstJoinerIDs);
            string strSql = "select CalID from " + _tableName + " where " + strType + "ID in (" + strJoinerIDs + ")";
            List<int> lstCalIDs = dbOper.Query<int>(strSql);
            return lstCalIDs;
        }
    }
}
